# -*- coding: utf-8 -*-
import   poplib   
import   cStringIO   
import   email   
import   base64
import   os
import   re
import   time
import   Printf


def pop3GetFiles(source,pop3,user,password):
        
        if not os.path.exists(source):
                os.mkdir(source) # make directory
                print 'Successfully created directory',source
        now=source+'\\'+time.strftime('%H%M%S')
        if not os.path.exists(now):
                os.mkdir(now) # make directory
                print 'Successfully created directory',now
        #POP3取信   
        M   =   poplib.POP3(pop3)   
        M.user(user)   
        M.pass_(password)   
    
        #打印有多少封信   
        numMessages   =   len(M.list()[1])   
        print   'num   of   messages',   numMessages
        print u'您触发了一个秘密程序！ OZK~~~'
        print u'请耐心等待,秘密马上揭晓！'
    
    
        for   i   in   range(numMessages):           
                m   =   M.retr(i+1)   
            
                buf   =   cStringIO.StringIO()   
                for   j   in   m[1]:                   
                        print   >>buf,   j   
                buf.seek(0)   
    
                  #解析信件内容   
                msg   =   email.message_from_file(buf)   
                for   part   in   msg.walk():   
                        contenttype   =     part.get_content_type()     
                        filename   =  part.get_filename() 
                        #不知道什么原因MailDesktop发出的附件类型是‘text/base64’所以加上了 or contenttype   ==   'text/base64'
                        if   filename and (contenttype   ==   'application/octet-stream' or contenttype   ==   'text/base64'):   
                                #   保存附件
                                #print  contenttype
                                f   =   open("%s"   %   (filename),'wb')   
                                f.write(base64.decodestring(part.get_payload()))   
                                f.close()
                                """
                        elif   contenttype   ==   'text/plain':   
                                #   保存正文   
                                f   =   open("mail%d.txt"   %   (i+1),'wb')   
                                f.write(base64.decodestring(part.get_payload()))   
                                f.close()
                                """
        





         
        path=os.getcwd()+'\\'+now
        try:
                zip_command='rename *.rar_gm *.rar'
                os.system(zip_command)
                zip_command='rar x %s\\*.rar  %s'  %(os.getcwd(),path)
                os.system(zip_command)
                zip_command='del e *.rar'
                os.system(zip_command)
        except EOFError:
                print '不存在单独压缩文件,程序继续'
        except:
                print '就不告诉你'
        
        
        
        zip_command='rename *.RAR_gm *.rar'  
        os.system(zip_command)
        for i in range(1,6):
                aiio=Printf.printf(i,1)
                zip_command='rar x %s\\*.zip.part%s.rar  %s'  %(os.getcwd(),aiio,path)
                if os.system(zip_command)==0:
                       #print u'解压成功'                        
                       break
                                      
                               
        zip_command='del e *.zip.part*.rar'
        os.system(zip_command)
        print u'哈哈,这个您发现了哈！附件已经帮你搞定放好getfile文件夹里了!请查看！'
        

    
                        
                
                
        
